<template>
<div>
  <a-modal
    :title="addFlag ? '新增组织机构' : '修改组织机构'"
    :visible="visible"
    @ok="handleSubmit"
    @cancel="handleClose"
    :confirmLoading="submitLoading"
    :maskClosable="false"
    :bodyStyle="{ padding: '12px 18px' }"
    :centered="true"
  >
    <a-form-model
      @submit="handleSubmit"
      :model="form"
    >
      <a-form-model-item style="display: none">
        <a-input v-model="form.id" />
      </a-form-model-item>
      <a-form-model-item label="父级组织">
        <a-tree-select
          v-model="form.parentOrgNo"
          placeholder="父级组织"
          style="width: 100%"
          :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
          :tree-data="organizationTree"
          
          allow-clear
          :replaceFields="{
            title: 'title',
            key: 'key',
            value: 'value',
            children:'children',
          }"
        >
        </a-tree-select>
      </a-form-model-item>
      <a-form-model-item label="组织名称">
        <a-input placeholder="组织名称" v-model="form.orgName" />
      </a-form-model-item>
      <a-form-model-item label="排序">
        <a-input-number
          style="width: 60%"
          placeholder="按数值由小到大升序"
          v-model = "form.sortNo"
        />
      </a-form-model-item>
      <a-form-model-item label="描述信息">
        <a-textarea placeholder="描述信息" v-model="form.descr" />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
  </div>
</template>

<script>

export default {
  name: 'OrganizationModal',
  data () {
    return {
      organizationTree:[],
      submitLoading: false,
      form: {
        id:"",
        orgNo:"",
        orgName:"",
        parentOrgNo:"",
        descr:"",
        sortNo:"",
      },
      addFlag:true,
      visible:false
    }
  },
  created(){
    this.init();
  },
  methods: {
    //获取select-tree数据，封装好
    init(){
      this.$http('/rapid/org/getOrgSelectTree').then(res =>{
        if(res.code === 200){
          this.organizationTree = res.result;
        }
      })
    },
    add(){
      this.visible = true;
      this.addFlag = true;
    },
    update(record){
      this.visible = true;
      this.addFlag = false;
      this.form = record;
    },
    handleSubmit(){
      this.$http('/rapid/org/addOrg',this.form).then(res =>{
        if(res.code === 200){
          this.$message.success('更新完成！')
        }
      })
    },
    handleClose(){
      this.visible = false;
      this.form = {};
    }
  }
}
</script>
